package ui.face
{

	import flash.display.Bitmap;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;

	import globals.ButtonType;
	import globals.Global;
	import globals.Setting;

	import manager.LinkManager;

	import ui.component.Button;
	import ui.component.ImageButton;
	import ui.component.Label;
	import ui.component.TextButton;
	import ui.panel.Alert;
	import ui.panel.PanelType;
	import ui.scene.BaseScene;

	import util.TextStyle;
	import util.UIUtil;

	public class BottomFace extends Sprite
	{
		private var _friendItems:Array;
		private var _menuItems:Array;
		private var _monsterWaveTotal:Label; //总共多少波怪。
		private var _refreshBtn:Button;
		private var _leftBtn:Button;
		private var _rightBtn:Button;

		public function BottomFace()
		{
			var bg:Bitmap;
			bg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("bottom"));
			bg.x = (Setting.STAGE_WIDTH - bg.width) * 0.5;
			this.addChild(bg);

			_friendItems = [];
			var friend:FriendCell;

			for(var i:uint = 0; i < 8; i++)
			{
				friend = new FriendCell();
				friend.x = 60 + 80 * i;
				friend.y = 43;
				this.addChild(friend);
				_friendItems.push(friend);
			}

			_menuItems = new Array();
			var actionIcon:ActionIcon;

			actionIcon = new ActionIcon("sc_warrior", "武将", heroListHandle);
			/*actionIcon = new ActionIcon("sc_warrior", "武将", actionIconHandler);
			   actionIcon.addItem("sc_train", "训练", trainHandler);
			 actionIcon.addItem("sc_presence", "装备", equipHandler);*/
			actionIcon.x = 80;
			actionIcon.y = -25;
			this.addChild(actionIcon);
			_menuItems.push(actionIcon);

			actionIcon = new ActionIcon("sc_equip", "装备", equipHandler);
			/*actionIcon.addItem("sc_equipon", "合成", rebuildHandler);
			 actionIcon.addItem("sc_strong", "强化", equipLevelUpHandler);*/
			actionIcon.x = 140;
			actionIcon.y = -25;
			this.addChild(actionIcon);
			_menuItems.push(actionIcon);

			actionIcon = new ActionIcon("sc_bag", "背包", actionIconHandler);
			actionIcon.addItem("sc_build", "建造", buildHandler);
			actionIcon.addItem("sc_distroy", "拆除", presenceHandler);
			actionIcon.x = 200;
			actionIcon.y = -25;
			this.addChild(actionIcon);
			_menuItems.push(actionIcon);

			actionIcon = new ActionIcon("sc_item", "购买", actionIconHandler);
			actionIcon.addItem("sc_trade", "购买", heroListHandle);
			actionIcon.addItem("sc_bag", "背包", levelupHandler);
			actionIcon.x = 260;
			actionIcon.y = -25;
			this.addChild(actionIcon);
			_menuItems.push(actionIcon);
			//
			_leftBtn = UIUtil.getButton(0, ButtonType.BUTTON_ARROW_LEFT, btnHandler);
			_leftBtn.x = 16;
			_leftBtn.y = 50;
			this.addChild(_leftBtn);
			_rightBtn = UIUtil.getButton(1, ButtonType.BUTTON_ARROW_RIGHT, btnHandler);
			_rightBtn.x = 700;
			_rightBtn.y = 50;
			this.addChild(_rightBtn);
			_refreshBtn = UIUtil.getButton(2, ButtonType.BUTTON_REFRESH, btnHandler);
			_refreshBtn.x = 700;
			_refreshBtn.y = 95;
			this.addChild(_refreshBtn);

			var attackBtn:Button = new Button(Global.assetsManager.getAssetsAsBitmapData("begin_attack"), null, null, null, btnHandler);
			attackBtn.id = 3;
			attackBtn.x = 620;
			attackBtn.y = -56;
			this.addChild(attackBtn);
			var collectBtn:TextButton = new TextButton("采集", Global.assetsManager.getAssetsAsBitmapData("collect_icon"), null, null, null, btnHandler);
			collectBtn.id = 4;
			collectBtn.x = 688;
			collectBtn.y = -262;
			collectBtn.isBorderColor = true;
			collectBtn.offsetY = 20;
			this.addChild(collectBtn);
			/*var effect:MovieClip = Global.assetsManager.getAssetsAsMovieClip("BladeGlow", "effect");
			   effect.x = 706;
			   effect.y = -32;
			 addChild(effect);*/

			_monsterWaveTotal = new Label(18, 0xffef02, true, "left", true, 0x000000);
			_monsterWaveTotal.x = 660;
			_monsterWaveTotal.y = -10;
			this.addChild(_monsterWaveTotal);
			//TODO：测试
			updataFriend(null);
			updateMonster("20");
		}

		private function btnHandler(button:Button):void
		{
			switch(button.id)
			{
				case 0: //左按钮
					Global.uiManager.optionPanel.openByPoint(new Point(200, 200));
					//Global.uiManager.openPanel(PanelType.OPTION_PANEL);
					break;
				case 1: //右按钮
					break;
				case 2: //刷新
					break
				case 3: //攻击
					Global.sceneManager.enter(BaseScene.BATTLE_SCENE);
					break;
				case 4: //采集
					break;
			}
		}

		private function actionIconHandler(item:ActionIcon):void
		{
			for each(var actionIcon:ActionIcon in _menuItems)
			{
				if(actionIcon != item || actionIcon.opened)
				{
					actionIcon.layout(false);
				}
				else
				{
					actionIcon.layout(true);
				}
			}
		}

		private function closeAllAction():void
		{
			for each(var actionIcon:ActionIcon in _menuItems)
			{
				actionIcon.layout(false);
			}
		}

		public function updateMonster(data:String):void
		{
			_monsterWaveTotal.text = data;
		}

		/**英雄列表**/
		private function heroListHandle(item:ActionIcon):void
		{
			Global.uiManager.warriorPanel.isDispatch = false;
			LinkManager.getHeroList();
		}

		private function levelupHandler():void
		{
			LinkManager.equipToStrong();
		}

		private function buildHandler():void
		{
			Global.uiManager.openPanel(PanelType.TOWER_PANEL);
		}

		private function rebuildHandler():void
		{
			Global.uiManager.openPanel(PanelType.TOWER_REBUILD_PANEL);
		}

		private function equipLevelUpHandler():void
		{
			LinkManager.equipToStrong();
		}

		private function presenceHandler():void
		{
			Global.uiManager.openPanel(PanelType.PRESENCE_PANEL);
		}

		private function equipHandler(item:ActionIcon):void
		{
			LinkManager.getEquipByHero();
		}

		private function trainHandler():void
		{
			Global.uiManager.openPanel(PanelType.TRAIN_PANEL);
		}

		public function updataFriend(data:Array):void
		{
			var friend:FriendCell;

			for(var i:int = 0; i < 8; i++)
			{
				friend = _friendItems[i];

				if(i < 4)
				{
					friend.update("remote/avatar1.png", "neaamo");
				}
				else
				{
					friend.clear();
				}
			}
		}
	}
}